package My;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Properties;

public class Serve {
    static ArrayList<Socket> list = new ArrayList<>();
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10086);
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream("C:\\Users\\Zdm\\桌面\\MyStudy\\Chat\\userInfo.txt");
        prop.load(fis);
        fis.close();

        while (true) {
            Socket socket = ss.accept();
            System.out.println("客户端来连接");
            new Thread(new MyRunnable(socket, prop)).start();
        }
    }
}

class MyRunnable implements Runnable {

    Socket socket;
    Properties prop;

    public MyRunnable(Socket socket, Properties prop) {
        this.socket = socket;
        this.prop = prop;
    }

    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while (true) {
                String choose = br.readLine();
                switch (choose) {
                    case "login" -> login(br);
                    case "register" -> System.out.println("注册");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void login(BufferedReader br) throws IOException {
        System.out.println("用户选择了登录");
        String userinfo = br.readLine();
        System.out.println(userinfo);
        String[] userinfoArr = userinfo.split("&");
        String usernameInput = userinfoArr[0].split("=")[1];
        String passwordInput = userinfoArr[1].split("=")[1];
        System.out.println("用户名" + usernameInput);
        System.out.println("密码" + passwordInput);
        if (prop.containsKey(usernameInput)) {
            String rightPassword = prop.get(usernameInput) + "";
            if (rightPassword.equals(passwordInput)) {
                writePassage2Client("1");
                Serve.list.add(socket);
                talk2All(br,usernameInput);
                while (true) {
                    //正在聊天的代码
                }
            } else {
                writePassage2Client("2");
            }
        } else {
            writePassage2Client("3");
        }
    }

    private void talk2All(BufferedReader br, String usernameInput) throws IOException {
        while (true){
            String msg = br.readLine();
            System.out.println(usernameInput + "发过来消息" + msg);
            for (Socket s : Serve.list) {
                writePassage2Client(s,usernameInput + "发过来消息" + msg);
            }
        }
    }

    private void writePassage2Client(Socket s, String s1) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        bw.write(s1);
        bw.newLine();
        bw.flush();
    }

    public void writePassage2Client(String message) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write(message);
        bw.newLine();
        bw.flush();
    }
}
